goog.provide('dkpoints.rpc.commands.AuthCheckCommand');
goog.provide('dkpoints.rpc.commands.AuthCheckResponse');

goog.require('dkpoints.rpc.commands.Command');
goog.require('dkpoints.rpc.commands.CommandResponse');

goog.require('goog.date.DateTime');
goog.require('goog.net.XhrIo');



/**
 * Command object to retrieve the current user's authentication status from
 * the server.
 * @param {function(dkpoints.rpc.commands.AuthCheckResponse)} onSuccess
 *    This function takes the processed response.
 * @param {function(goog.events.Event)} onFailure Return the raw XHR response
 *    event so the caller can diagnose the problem.
 * @constructor
 * @extends {dkpoints.rpc.commands.Command}
 */
dkpoints.rpc.commands.AuthCheckCommand = function(onSuccess, onFailure) {
  var commandID = 'AuthCheck:' + goog.now(),
      url = '/apis/auth/check/';

  dkpoints.rpc.commands.Command.call(this,
                                     onSuccess, onFailure, commandID, url);
};
goog.inherits(dkpoints.rpc.commands.AuthCheckCommand,
              dkpoints.rpc.commands.Command);


/**
 * The callback to be executed when the XhrManager dispatches an onSuccess
 * event.  This method creates a new AuthCheckResponse and passes it to the
 * original caller's onSuccess callback.
 * @param {goog.net.XhrManager.Event} event The XHR COMPLETE event.
 */
dkpoints.rpc.commands.AuthCheckCommand.prototype.onSuccess = function(event) {
  var response = event.target.getResponseJson();
  if (goog.isDefAndNotNull(response)) {
    var authStatus =
        new dkpoints.rpc.commands.AuthCheckResponse(response);

    this.callersOnSuccess_(authStatus);
  }
  else {
    this.onFailure(event);
  }
};


/**
 * The callback to be executed when the XhrManager dispatches an onError event.
 * This method simply passes the event object back to the command's
 * callerOnFailure method.
 * @param {goog.net.XhrManager.Event} event The XhrManager event.
 */
dkpoints.rpc.commands.AuthCheckCommand.prototype.onFailure = function(event) {
  this.callersOnFailure_(event);
};



/**
 * CommandResponse object to receive the server's JSON response and pick it
 * out.  This can be passed an empty object to return a default auth status.
 * @param {Object} serverResponse The parsed JSON object returned by the server.
 * @constructor
 * @extends {dkpoints.rpc.commands.CommandResponse}
 */
dkpoints.rpc.commands.AuthCheckResponse = function(serverResponse) {
  /** @type {boolean} */
  this.loggedIn = serverResponse['loggedIn'] || false;

  /** @type {boolean} */
  this.isAdmin = serverResponse['isAdmin'] || false;

  /** @type {string} */
  this.character = serverResponse['character'] || '';
};
goog.inherits(dkpoints.rpc.commands.AuthCheckResponse,
              dkpoints.rpc.commands.CommandResponse);


/**
 * @return {dkpoints.auth.AuthState} The state of the auth status object.
 */
dkpoints.rpc.commands.AuthCheckResponse.prototype.getState = function() {
  return this.loggedIn ? (this.isAdmin ? dkpoints.auth.AuthState.ADMIN_USER :
                          dkpoints.auth.AuthState.NORMAL_USER) :
                         dkpoints.auth.AuthState.NO_AUTH;
};
